package yang.itcode.controller;

import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yang.itcode.model.Result;
import yang.itcode.service.SystemSettingService;

import static yang.itcode.constant.ErrorConstant.*;

@RestController
@RequestMapping("/api/setting")
public class SystemSettingController {

    @Autowired
    private SystemSettingService systemSettingService;

    @GetMapping()
    public Result<JSONObject> getSystemSetting() {
        JSONObject systemSetting = systemSettingService.getSystemSetting();
        return Result.success(systemSetting);
    }

    @PutMapping("/imageAddress")
    public Result updateImageAddress(@RequestBody JSONObject obj) {
        String imageAddress = obj.getStr("imageAddress");
        if (systemSettingService.updateImageAddress(imageAddress)){
            return Result.success();
        }else {
            return Result.error(SYSTEM_SETTING_EXCEPTION_CODE,SYSTEM_SETTING_IMAGE_EXCEPTION_MESSAGE);
        }
    }
}
